﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Diagnostics;

namespace GraphicSVCUtil
{
    class SVCUTILexec
    {

        #region //public variables

            public string strClassName
            {
                set { _strClassName = value; }
            }

            public string strExecutionPath
            {
                get { return _strExecutionPath; }
            }

            public string strWSDLInputFilePath
            {
                set { _strWSDLInputFilePath = value; }
            }

            public string strConfigFile
            {
                set { _strConfigFile = value; }
            }

            public string strOutputPath
            {
                set { _strOutputPath = value; }
            }

            public bool bUseCSharp
            {
                set { _bUseCSharp = value; }
            }
        #endregion

        #region //private variables

            private string _strExecutionPath;
            private string _strClassName;
            private string _strWSDLInputFilePath;
            private string _strConfigFile;
            private string _strOutputPath;
            private bool _bUseCSharp;
 
        #endregion

        /// <summary>
        /// default constractor
        /// </summary>
        public SVCUTILexec()
        {
            _strExecutionPath = ConfigurationSettings.AppSettings["executionPath"];
        }

        /// <summary>
        /// Public function to execute svcutil
        /// </summary>
        public void SVCUTILExecute()
        {
            //constracting the execution command
            string strCommand = "/language:";
            if (_bUseCSharp)
                strCommand += "c#";
            else
                strCommand += "vb";
            strCommand += " /out:\"" + _strOutputPath + "\\" + _strClassName;
            if (_bUseCSharp)
                strCommand += ".cs\" ";
            else
                strCommand += ".vb\" ";
            strCommand += "\"" + _strWSDLInputFilePath + "\"";

            //executing stage
            ProcessStartInfo procStartInfo = new ProcessStartInfo();//("cmd", "/c " + strCommand);

            // The following commands are needed to redirect the standard output.
            // This means that it will be redirected to the Process.StandardOutput StreamReader.
            procStartInfo.RedirectStandardOutput = true;
            procStartInfo.UseShellExecute = false;
            // Do not create the black window.
            procStartInfo.CreateNoWindow = true;
            //execution file and arguments
            procStartInfo.FileName = _strExecutionPath + "\\svcutil.exe";
            procStartInfo.Arguments = strCommand;
            // Now we create a process, assign its ProcessStartInfo and start it
            Process proc = new Process();
            proc.StartInfo = procStartInfo;
            proc.Start();

            proc.Close();

        }
    }//end of class
}
